using UnityEngine;
using UnityEngine.UI;

namespace FutureCorePlugin
{
    public class FillCanvasRootAdaptive : MonoBehaviour
    {
        private RectTransform canvasRectTF;
        private RectTransform fillRectTF;

        private RectTransform topTF;
        private RectTransform bottomTF;
        private RectTransform leftTF;
        private RectTransform rightTF;

        private void Awake()
        {
            canvasRectTF = GetComponent<RectTransform>();
            Init();
            Adaptive();
        }

#if UNITY_EDITOR
        private float currScreenHeight;
        private float currScreenWidth;

        private void Update()
        {
            if (currScreenHeight != Screen.height || currScreenWidth != Screen.width)
            {
                currScreenHeight = Screen.height;
                currScreenWidth = Screen.width;
                AdaptiveConst.CurrAspectRatio = (float)Screen.height / Screen.width;
                Adaptive();
            }
        }
#endif

        private void Init()
        {
            GameObject fillRect = new GameObject("FillRect");
            fillRect.layer = PLayerMaskConst.UI;
            fillRectTF = fillRect.AddComponent<RectTransform>();
            fillRectTF.SetParent(canvasRectTF, false);

            topTF = CreateFillItem("topTF");
            bottomTF = CreateFillItem("bottomTF");
            leftTF = CreateFillItem("leftTF");
            rightTF = CreateFillItem("rightTF");

            topTF.pivot = new Vector2(0.5f, 1f);
            bottomTF.pivot = new Vector2(0.5f, 0f);
            leftTF.pivot = new Vector2(0f, 0.5f);
            rightTF.pivot = new Vector2(1f, 0.5f);
            topTF.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
            bottomTF.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 0);
            leftTF.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
            rightTF.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 0);
        }

        private RectTransform CreateFillItem(string name)
        {
            GameObject go = new GameObject(name);
            go.layer = PLayerMaskConst.UI;
            RectTransform rtf = go.AddComponent<RectTransform>();
            rtf.SetParent(fillRectTF, false);
            Image image = rtf.gameObject.AddComponent<Image>();
            image.color = Color.black;
            rtf.gameObject.SetActive(false);
            return rtf;
        }

        private void Adaptive()
        {
            if (canvasRectTF == null) return;

            UIUtils.SetUIFullScreen(canvasRectTF);
            UIUtils.SetUIFullScreen(fillRectTF);

            if (AdaptiveConst.CurrAspectRatio > AdaptiveConst.StandardAspectRatio)
            {
                float scale = AdaptiveConst.StandardResolution.x / Screen.width;
                float height = (Screen.height * scale - AdaptiveConst.StandardResolution.y) / 2;
                FillVertical(topTF, height);
                FillVertical(bottomTF, height);
                leftTF.gameObject.SetActive(false);
                rightTF.gameObject.SetActive(false);
            }
            else
            {
                float scale = AdaptiveConst.StandardResolution.y / Screen.height;
                float width = (Screen.width * scale - AdaptiveConst.StandardResolution.x) / 2;
                FillHorizontal(leftTF, width);
                FillHorizontal(rightTF, width);
                topTF.gameObject.SetActive(false);
                bottomTF.gameObject.SetActive(false);
            }
        }

        private void FillVertical(RectTransform tf, float height)
        {
            tf.sizeDelta = new Vector2(AdaptiveConst.StandardResolution.x, height);
            tf.gameObject.SetActive(true);
        }

        private void FillHorizontal(RectTransform tf, float width)
        {
            tf.sizeDelta = new Vector2(width, AdaptiveConst.StandardResolution.y);
            tf.gameObject.SetActive(true);
        }
    }
}